package com.lan.app.service.impl;

import com.lan.app.dao.TpDao;
import com.lan.app.repository.Tp;
import com.lan.app.service.ITpService;
import com.lan.support.page.Page;
import com.lan.support.page.PageHelper;
import com.lan.support.response.ex.Ex;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
public class TpService implements ITpService {

    @Resource
    private TpDao tpDao;

    @Override
    public Integer save(Tp tp) {
        if (tp.getTbId() == null) {
            throw Ex.build("表ID不能为空");
        }
        Date curDate = new Date();
        tp.setState(1);
        tp.setIsDel(1);
        tp.setCreateTime(curDate);
        tp.setUpdateTime(curDate);
        tpDao.save(tp);
        Integer id = tp.getId();
        return id;
    }

    @Override
    public int del(Integer id) {
        int result = tpDao.del(id);
        return result;
    }

    @Override
    public int modifyDel(Integer id) {
        int result = tpDao.modifyDel(id, 0, new Date());
        return result;
    }

    public int updateById(Tp Tp) {
        if (Tp.getId() == null) {
            throw Ex.build("ID 不能为空");
        }
        int result = tpDao.updateById(Tp);
        return result;
    }

    @Override
    public Tp get(Integer id) {
        Tp Tp = tpDao.findById(id);
        return Tp;
    }

    @Override
    public Page<Tp> page(PageHelper helper, Tp tp) {
        int count = tpDao.countByPage(tp, helper.getPage());
        Page<Tp> page = new Page<>(helper, count);
        if (count == 0) {
            page.setContent(null);
        } else {
            List<Tp> list = tpDao.findByPage(tp, helper.getPage());
            page.setContent(list);
        }
        return page;
    }

    @Override
    public List<Tp> getList(Integer tbId) {
        return tpDao.getList(tbId);
    }
}